package com.mazegame.frontend;

import com.mazegame.backend.ImageDisplayPane;
import com.mazegame.test.Maze3;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javax.swing.*;
import java.awt.*;

public class MazeGeneratePanel2 extends JPanel implements GamePanel {
    private static CardLayout cardLayout;
    private static JPanel cardPanel;
    private JLayeredPane layeredPane;
    private static final int WIDTH = 1500;
    private static final int HEIGHT = 900;
    private JFXPanel jfxPanel;
    public static boolean isFirst = true;

    public MazeGeneratePanel2(CardLayout cardLayout, JPanel cardPanel) {
        this.cardLayout = cardLayout;
        this.cardPanel = cardPanel;
        initializeUI();
    }
    // 添加背景图的方法
    private Pane addBackground(Pane content, String imagePath) {
        ImageView background = new ImageView(new Image(getClass().getResourceAsStream(imagePath)));
        background.setFitWidth(WIDTH);
        background.setFitHeight(HEIGHT);
        StackPane stackPane = new StackPane();
        stackPane.getChildren().addAll(background, content);
        return stackPane;
    }
    private void initializeUI() {
        setLayout(new BorderLayout());
        // 初始化JavaFX运行环境
        jfxPanel = new JFXPanel();
        add(jfxPanel, BorderLayout.CENTER);
        // 在JavaFX应用线程中设置场景
        Platform.runLater(() -> {
            try {
                BorderPane contentPane = createContentPane();
                Pane backgroundPane = addBackground(contentPane, "/img/黑色背景.png");
                Scene scene = new Scene(backgroundPane, WIDTH, HEIGHT);
                jfxPanel.setScene(scene);
            } catch (Exception e) {
                e.printStackTrace();
                // 错误处理：显示错误消息
                SwingUtilities.invokeLater(() -> {
                    JOptionPane.showMessageDialog(
                            this,
                            "加载界面失败: " + e.getMessage(),
                            "错误",
                            JOptionPane.ERROR_MESSAGE
                    );
                });
            }
        });
    }
    private BorderPane createContentPane() {
        BorderPane root = new BorderPane();
        javafx.scene.control.Label tip = new Label("金币模式：点击任意位置开始...");
        tip.setFont(Font.font("KaiTi", FontWeight.BOLD, 48));
        tip.setTextFill(Color.WHITE);
        root.setOnMouseClicked(event -> {
            if(isFirst){
                new com.sun.j3d.utils.applet.MainFrame(new Maze3(), 1500, 900);
                isFirst = false;
            }
        });
        root.setTop(createTitleRegion());
        root.setCenter(tip);
        return root;
    }

    private Region createTitleRegion() {
        HBox titleBox = new HBox();
        titleBox.setPadding(new Insets(20, 0, 0, 30));
        // 设置宽高
        titleBox.setPrefSize(WIDTH, 120);
        // 设置背景颜色,辅助布局
        //titleBox.setStyle("-fx-background-color: rgb(255,255,255);");
        javafx.scene.control.Button button = new Button();
        ImageDisplayPane imageDisplayPane = new ImageDisplayPane("/img/返回按钮.png", 80, 80, 0);
        button.setGraphic(imageDisplayPane);
        button.setStyle("-fx-background-color:transparent; -fx-background-radius: 200;");
        button.setEffect(null);
        button.setPrefSize(100, 100);
        // 设置最大宽高度
        button.setMaxSize(100, 100);
        // 添加悬停效果
        button.setOnMouseEntered(e -> {
            button.setStyle("-fx-background-color: rgba(255,255,255,0.5); -fx-background-radius: 200;");
        });
        button.setOnMouseExited(e -> {
            button.setStyle("-fx-background-color: transparent; -fx-background-radius: 200;");
            button.setEffect(null);
        });
        // 添加点击效果
        button.setOnAction(e -> {
            cardLayout.show(cardPanel, "OPTIONPANEL");
        });
        titleBox.getChildren().add(button);
        return titleBox;
    }

    @Override
    public void showPanel() { cardLayout.show(cardPanel, "MAZEGENERATEPANEL2");}

    @Override
    public void cleanup() {

    }
}
